define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'product/index' + location.search,
                    add_url: 'product/add',
                    edit_url: 'product/edit',
                    del_url: 'product/del',
                    multi_url: 'product/multi',
                    import_url: 'product/import',
                    table: 'product',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'en_title', title: __('En_title'), operate: 'LIKE'},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'price', title: __('Price')},
                        {field: 'cate.title', title: __('Cate_id')},
                        {field: 'series.title', title: __('Series_id')},
                        {
                            field: 'weigh', // weigh 列
                            title: '排序',
                            editable: true, // 使该列可编辑
                            sortable: true, // 可排序
                            align: 'center', // 居中显示
                            formatter: function (value, row, index) {
                                // 格式化编辑框，显示当前值
                                return '<input style="width: 40px;outline: none;text-align: center;border:1px solid #d0d0d0;" type="text" class="weigh-input" value="' + value + '" data-id="' + row.id + '" />';
                            },
                            events: {
                                'blur .weigh-input': function (e, value, row, index) {
                                    // 当输入框失去焦点时更新 weigh
                                    var newWeigh = $(this).val();
                                    var id = row.id;

                                    // 发送 AJAX 请求更新 weigh 值
                                    Fast.api.ajax({
                                        url: Fast.api.fixurl('product/updateWeigh'),
                                        type: 'POST',
                                        loading: false,
                                        data: {
                                            id: id,
                                            weigh: newWeigh
                                        },
                                        success: function (response) {
                                            if(response.code==1){
                                                Fast.api.msg('更新成功');
                                            }else{
                                                Fast.api.msg('更新失败');
                                            }
                                        },
                                        error: function () {
                                            Fast.api.msg('请求失败');
                                        }
                                    });
                                }
                            }
                        },
                        {field: 'hot_switch', title: __('Hot_switch'), searchList: {"1":__('Yes'),"0":__('No')}, table: table, formatter: Table.api.formatter.toggle},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
